<template>
  <div>
    <div class="breadcrumb-wrap">
      <el-breadcrumb>
        <el-breadcrumb-item :to="{ path: '/list' }">数据列表</el-breadcrumb-item>
        <el-breadcrumb-item>新建数据</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <el-form :model="form" ref="form" label-width="100px" class="demo-ruleForm">

      <el-form-item label="姓名" prop="D_Name" :rules='[{ required: true, message: "姓名不能为空" }]'>
        <el-input type="name" v-model="form.D_Name" auto-complete="off"></el-input>
      </el-form-item>

      <el-form-item label="D_Code" prop="D_Code" :rules='[
          { required: true, message: "D_Code不能为空" },
          { type: "number", message: "D_Code必须为数字值" }
        ]'>
        <el-input type="number" v-model.number="form.D_Code" auto-complete="off"></el-input>
      </el-form-item>

      <el-form-item label="D_Sort" prop="D_Sort" :rules='[
          { required: true, message: "D_Sort不能为空" },
          { type: "number", message: "D_Sort必须为数字值" }
        ]'>
        <el-input type="number" v-model.number="form.D_Sort" auto-complete="off"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('form')">提交</el-button>
      </el-form-item>

    </el-form>
  </div>
</template>

  <script>
import Config from "@/config/config.js";
export default {
  data() {
    return {
      pageindex: this.$route.query.pageindex || "",
      form: {
        type: "addormodifydicmodel",
        flagtype: this.$route.query.objid ? 2 : 1,
        objid: this.$route.query.objid || "",
        D_Name: "",
        D_Code: "",
        D_Sort: "",
        D_IsDelete: 0,
        D_ParentID: "",
        D_TypeID: ""
      }
    };
  },
  methods: {
    async getInfo() {
      let _this = this;
      let params = {
        type: "getordeldicmodel",
        flagtype: 1,
        objid: _this.form.objid
      };

      let r = await _this.$http.get(Config.sync, {
        params: params
      });

      if (!r || !r.body.Result) {
        _this.$message.error("获取数据失败");
        _this.$router.push({ path: "/list" });
        return false;
      }

      let data = r.body.Data.data;

      for (let i in data) {
        if (i == "D_Code") {
          _this.form[i] = parseInt(data[i]);
        } else {
          _this.form[i] = data[i];
        }
      }
      
      _this.form.D_IsDelete = _this.form.D_IsDelete ? 1 : 0;
      console.log(_this.form);
    },

    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.addData();
        } else {
          this.$message.error("请填完必填项");
          return false;
        }
      });
    },

    async addData() {
      let _this = this;
      let r = await _this.$http.get(Config.sync, {
        params: _this.form
      });

      if (!r || !r.body.Result) {
        _this.$message.error("创建数据失败，请稍后重试");
        return false;
      }

      _this.$router.push({
        path: "/list",
        query: { pageindex: _this.pageindex }
      });
    }
  },
  mounted() {
    if (this.$route.query.objid) {
      this.getInfo();
    }
  }
};
</script>

<style>
.demo-ruleForm {
  width: 400px;
  margin-top: 20px;
}
</style>
